package com.cong.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cong.project.mapper.LikeMapper;
import com.cong.project.model.entity.CollectNum;
import com.cong.project.model.entity.LikeNum;
import com.cong.project.model.entity.Likes;
import com.cong.project.model.enums.ContentTypeEnum;
import com.cong.project.model.enums.LikeStatusEnum;
import com.cong.project.model.request.TokenUserRequest;
import com.cong.project.service.CollectNumService;
import com.cong.project.service.LikeNumService;
import com.cong.project.service.LikeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author 86188
* @description 针对表【like(点赞表)】的数据库操作Service实现
* @createDate 2022-10-31 15:22:24
*/
@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Likes>
    implements LikeService{

    @Resource
    private LikeService likeService;
    @Resource
    private LikeNumService likeNumService;
    @Resource
    private CollectNumService collectNumService;
    @Override
    public Integer thumbUp(TokenUserRequest tokenUserRequest, Long postId, Integer type) {
        Long userId = tokenUserRequest.getId();

        Likes likesResult = likeService.getOne(new LambdaQueryWrapper<Likes>().eq(Likes::getUserId, userId).eq(Likes::getContentId, postId)
                .eq(Likes::getType, type));

        synchronized (String.valueOf(userId).intern()) {
            //还没有点赞记录
            if (likesResult == null) {
                Likes likes = new Likes();
                likes.setContentId(postId);
                likes.setType(type);
                likes.setUserId(userId);
                likes.setLikeStatus(LikeStatusEnum.IS_LIKE.getCode());
                likeService.save(likes);
                boolean update = likeNumService.update(new UpdateWrapper<LikeNum>().eq("content_id", postId)
                        .eq("type", type).setSql("like_num=like_num+1"));
                return update ? 1 : 0;
            } else {
                //帖子已经点赞
                if (likesResult.getLikeStatus() == LikeStatusEnum.IS_LIKE.getCode()) {
                    likeService.update(new UpdateWrapper<Likes>().eq("content_id", postId)
                            .eq("user_id", userId)
                            .eq("type", type).setSql("like_status=0"));
                    boolean update = likeNumService.update(new UpdateWrapper<LikeNum>().eq("content_id", postId)
                            .eq("type", type).setSql("like_num=like_num-1"));
                    return update ? -1 : 0;

                }
                //帖子未点赞
                if (likesResult.getLikeStatus() == LikeStatusEnum.NO_LIKE.getCode()) {
                    likeService.update(new UpdateWrapper<Likes>().eq("content_id", postId)
                            .eq("user_id", userId)
                            .eq("type", type).setSql("like_status=1"));
                    boolean update = likeNumService.update(new UpdateWrapper<LikeNum>().eq("content_id", postId)
                            .eq("type", type).setSql("like_num=like_num+1"));
                    return update ? 1 : 0;


                }
            }
        }
        return 0;

    }

    @Override
    public Boolean saveThumbCollect(Long contentId, Integer type) {
        LikeNum likeNum = new LikeNum();
        likeNum.setContentId(contentId);
        likeNum.setType(type);
        //保存点赞数量信息
        boolean likeNumSave = likeNumService.save(likeNum);

        CollectNum collectNum = new CollectNum();
        collectNum.setContentId(contentId);
        collectNum.setType(type);
        //保存收藏数量信息
        boolean collectNumSave = collectNumService.save(collectNum);
        if (likeNumSave&&collectNumSave){
            return true;
        }
        return false;

    }


}




